#!/bin/bash
# 使用if 和 case 编写一个石头,剪子,布 游戏.

echoMenu(){
    echo "#####石头剪子布游戏#######"
    echo "#1. 石头"
    echo "#2. 剪子"
    echo "#3. 布"
    echo "##########################"
}

compare(){
    result=$[$1-$2]
    case $result in 
    0)
        return 0;;
    -1)
        return 1;;
    1)
        return 2;;
    2)
        return 1;;
    *)
        return 2;;
    esac
}

echoTip(){
    echo -n "$1出"
    if [[ $2 -eq 1 ]];then
        echo  "石头"
    elif [[ $2 -eq 2 ]];then
        echo  "剪子"
    else
        echo  "布"
    fi
}
main(){
    while true 
    do
        echoMenu
        computer=$[$RANDOM%3+1]
        read -p "请出:" human
        case $human in
        1)
            ;;
        2)
            ;;
        3)
            ;;
        *)
            echo -e "\e[31m选择错误\e[0m"
            exit 1;;
        esac
        echoTip "计算机" $computer
        echoTip "你" $human
        compare $computer $human
        result=$?
        case $result in 
        0)
            echo -e "\e[32m平局\e[0m";;
        1)
            echo -e "\e[32m计算机赢\e[0m";;
        *)
            echo -e  "\e[32m你赢了\e[0m";;
        esac
    done
}
main 
